import { addGoodToDonor } from "./function.js"
import { getLocalDate } from "../../utils/getLocalDate.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    true_money: 0,
    money: 0,
    number1: 1,
    disabled1: false,
    disabled2: false,
    good:{},
    showDialog: false,
  },

  
  async donateItem(){
    this.setData({
      showIndex:null,
      showDialog: true,
      
    })
    console.log(this.data.good);
    console.log(this.data.number1);
    var amount =this.data.number1;
    var date = await getLocalDate();
    // console.log(date);
    // JSON.stringify()
    var res=await addGoodToDonor("ourOx68K-yzP1JGUbwnM9FAEJ8hA",this.data.good,amount,(date));
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    if (options.goodInfo) {
      var goodInfo =JSON.parse(decodeURIComponent(options.goodInfo));
      console.log(goodInfo); 
      //console.log(goodInfo.name);
      var true_money=goodInfo.price
      this.setData({
        good: goodInfo,
        true_money:true_money,
        money:true_money,
      });
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },

  openPopup(e){
    var index = e.currentTarget.dataset.index;
    this.setData({
      showIndex:index
    })
  },
  closePopup(){
    this.setData({
      showIndex:null
    })
  },
  prevNum1() {
    this.setData({ 
      true_money: (this.data.number1+1)*this.data.money,
      number1: this.data.number1 >= 10 ? 10 : this.data.number1 + 1 ,
      disabled1: this.data.number1 !== 0 ? false : true,
      disabled2: this.data.number1 !== 10 ? false : true,
    });
    console.log(this.data.number1)
  },
  nextNum1() {
    this.setData({ 
      true_money: (this.data.number1-1)*this.data.money,
      number1: this.data.number1 <= 0 ? 0 : this.data.number1 - 1 ,
      disabled1: this.data.number1 !== 0 ? false : true,
      disabled2: this.data.number1 !== 10 ? false : true,
    });
   
  }

})